home *** CD-ROM | disk | FTP | other *** search
- /*
-
- simple 3D2-to-obj converter - 6/19/93 by Francisco X DeJesus
-
- dejesus@c3ot.saic.com - dejesus@avalon.chinalake.navy.mil
-
- (written under SunOS 4.1.3... tweaks may be necessary for the
- date/time stuff to work, but it is not crucial, so they can be
- commented out and a fake date hard-coded if neccesary).
-
- This program is provided "as-is" (ie: use it at your own risk!).
- Permission is hereby granted to redistribute this program freely,
- provided that this header is left unmodified. If you find any bugs or
- modify the program to improve it in any way, please let me know...
-
- If you have any interesting 3D objects/models, in any format, please
- upload them to the anonymous ftp archive avalon.chinalake.navy.mil!!!
-
- */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/timeb.h>
- #include <sys/time.h>
- #include <strings.h>
- #include <string.h>
-
- static char *months[]=
- {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
- };
-
- static char *days[]=
- {
- "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
- };
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- FILE *fpn,*fpo;
- struct timeval now;
- struct timezone zone;
- time_t then;
- struct tm *t;
- char infilename[40];
- char outfilename[40];
- char oname[9];
- char cx, cy, cz;
- unsigned char discard[1000], objname[10];
- unsigned short int objects[2], tobj, ofaces[2], overtices[2];
- unsigned short int f1, f2, f3, of1[2], of2[2], of3[2];
- short int vx[2], vy[2], vz[2];
- unsigned int vertices, faces;
- unsigned int i, v, f;
- unsigned long int vtotal, ftotal, cvtotal;
- float x, y, z;
-
- if (argc == 1)
- {
- printf("3D2 file to be converted? ");
- scanf("%s",infilename);
- printf("Obj file to output? ");
- scanf("%s",outfilename);
- if ((fpn = fopen(infilename,"r")) == 0) {
- printf ("file does not exist: %s\n", infilename);
- exit ();
- }
- if ((fpo = fopen(outfilename,"w")) == 0) {
- printf ("file cannot be created: %s\n", outfilename);
- exit ();
- }
- }
- else if (argc == 2)
- {
- if (strcmp (argv[1], "-h") == 0) {
- printf("usage:\n");
- printf(" tpoly2obj [inputfile.tpoly [outputfile.obj]]\n");
- printf("Please use full filenames. If files are not speficied on the\n");
- printf("command line, the program will prompt you for them.\n");
- exit();
- }
- printf("Obj file to output? ");
- scanf("%s",outfilename);
- if ((fpn = fopen(argv[1],"r")) == 0) {
- printf ("file does not exist: %s\n", argv[1]);
- exit ();
- }
- if ((fpo = fopen(outfilename,"w")) == 0) {
- printf ("file cannot be created: %s\n", outfilename);
- exit ();
- }
- }
- else
- {
- if ((fpn = fopen(argv[1],"r")) == 0) {
- printf ("file does not exist: %s\n", argv[1]);
- exit ();
- }
- if ((fpo = fopen(argv[2],"w")) == 0) {
- printf ("file cannot be created: %s\n", argv[2]);
- exit ();
- }
- }
-
- /* convert! */
-
- if (gettimeofday(&now,&zone)<0)
- perror("gettimeofday failed"), exit(2);
-
- if ((t=localtime(&now))==NULL)
- perror("localtime failed"), exit(3);
-
- if (t->tm_mday > 9)
- fprintf(fpo,"# %s %s %2.2d %2.2d:%2.2d:%2.2d %4.4d\n",
- days[t->tm_wday],months[t->tm_mon],t->tm_mday,
- t->tm_hour,t->tm_min,t->tm_sec,
- (1900+t->tm_year));
- if (t->tm_mday < 10)
- fprintf(fpo,"# %s %s %d %2.2d:%2.2d:%2.2d %4.4d\n",
- days[t->tm_wday],months[t->tm_mon],t->tm_mday,
- t->tm_hour,t->tm_min,t->tm_sec,
- (1900+t->tm_year));
-
- fprintf(fpo,"#\n");
- fprintf(fpo,"# Object converted by 3D2-to-obj\n");
- fprintf(fpo,"#\n\n");
-
- printf("Starting conversion...\n\n");
-
-
- fprintf(fpo,"g\n");
-
-
- /* read header ... discard everything but the number of objects */
-
- fread(discard,1,2,fpn);
-
- fread(objects,1,2,fpn);
-
- fread(discard,1,252,fpn); /* should be 316, -64??? */
-
- tobj=objects[0];
- printf("%d objects in .3d2 file...\n\n",tobj);
-
- /* main loop one, once for each object - write out vertex list*/
-
- vtotal=0;
- ftotal=0;
- cvtotal=0;
- for (i=1;i<=tobj;i++) {
- printf(" object #%d\n",i);
-
- fread(objname,1,9,fpn);
-
- printf(" object name: %s\n",objname);
-
- /* vertex loop - write out */
-
- fread(overtices,1,2,fpn);
- vertices=((overtices[1]>>8)+overtices[0]);
- vtotal=vtotal+vertices;
-
- printf(" vertices: %d\n",vertices);
-
- for (v=1;v<=vertices;v++) {
-
- fread(vx,1,2,fpn);
- fread(vy,1,2,fpn);
- fread(vz,1,2,fpn);
-
- x=((float)vx[0])/100;
- y=((float)vy[0])/100;
- z=((float)vz[0])/100;
-
- fprintf(fpo,"v %f %f %f\n",x,y,z);
- }
-
- /* face loop - discard */
-
- fread(ofaces,1,2,fpn);
- faces=((ofaces[1]>>8)+ofaces[0]);
- ftotal=ftotal+faces;
-
- printf(" faces: %d\n",faces);
-
- for (f=1;f<=faces;f++) {
- fread(discard,1,8,fpn);
- }
-
- printf("\n");
- }
-
- /* end of main loop one */
-
-
- printf("total: %d vertices , %d faces\n",vtotal,ftotal);
-
- fprintf(fpo,"# %d vertices\n\n",vtotal);
-
- fprintf(fpo,"# 0 vertex parms\n\n");
-
- fprintf(fpo,"# 0 texture vertices\n\n");
-
- fprintf(fpo,"# 0 normals\n\n");
-
- rewind(fpn);
-
- /* discard header altogether the second time around... */
-
- fread(discard,1,256,fpn);
-
-
- /* main loop two, once for each object - write out faces/group list*/
-
- for (i=1;i<=tobj;i++) {
- fread(objname,1,9,fpn);
-
- fprintf(fpo,"g %s\n",objname);
-
- /* vertex loop- discard */
-
- fread(overtices,1,2,fpn);
- vertices=((overtices[1]>>8)+overtices[0]);
-
- for (v=1;v<=vertices;v++) {
- fread(discard,1,6,fpn);
- }
-
- /* face loop - write to file */
-
- fread(ofaces,1,2,fpn);
- faces=((ofaces[1]>>8)+ofaces[0]);
-
- for (f=1;f<=faces;f++) {
- fread(of1,1,2,fpn);
- fread(of2,1,2,fpn);
- fread(of3,1,2,fpn);
- f1=(((of1[1]>>8)+of1[0])+1+cvtotal);
- f2=(((of2[1]>>8)+of2[0])+1+cvtotal);
- f3=(((of3[1]>>8)+of3[0])+1+cvtotal);
- fread(discard,1,2,fpn);
- fprintf(fpo,"f %d %d %d\n",f1,f2,f3);
-
- }
- cvtotal=cvtotal+vertices;
- }
-
- /* end of main loop two */
-
-
- fprintf(fpo,"# %d elements\n",ftotal);
-
- fclose(fpn);
- fclose(fpo);
- }
-